package com.yanqu.road.server.manger.activity.guagifts.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.dao.impl.player.UserGuaNobleRelationDaoImpl;
import com.yanqu.road.dao.impl.player.UserGuaRelationDaoImpl;
import com.yanqu.road.entity.activity.guagifts.UserGuaNobleRelationData;
import com.yanqu.road.entity.activity.guagifts.UserGuaRelationData;
import com.yanqu.road.pb.activity.GuaGiftsProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.guagifts.GuaGiftsModule;
import com.yanqu.road.server.manger.activity.guagifts.GuaGiftsMgr;
import com.yanqu.road.server.manger.activity.qixi.QixiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.sync.message.SyncResponse;

import java.util.Map;

public class GuaGetMostGiftTargetUserServiceCmd implements IGameServiceCommand {
    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        GuaGiftsProto.GuaGiftsGetMostGiftTargetUserReqMsg reqMsg = GuaGiftsProto.GuaGiftsGetMostGiftTargetUserReqMsg.parseFrom(request.getDataBytes());

        long targetUserId = reqMsg.getTargetUserId();
        int giftType = reqMsg.getGiftType();

        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(targetUserId);
        long giftMostUserId = 0;
        if (onlinePlayer != null) {
            giftMostUserId = onlinePlayer.getModule(GuaGiftsModule.class).getGiftMostUserId(giftType);
        }else{
            if (giftType == 1){
                Map<Long, UserGuaNobleRelationData> userRelationDataMap = new UserGuaNobleRelationDaoImpl().getUserGuaNobleRelationData(GuaGiftsMgr.getConfig().getActivityId(), targetUserId);
                giftMostUserId = GuaGiftsMgr.getNobleGiftMostUserIdByRelationMap(userRelationDataMap);
            }else{
                Map<Long, UserGuaRelationData> userGuaRelationDataMap = new UserGuaRelationDaoImpl().getUserGuaRelationData(GuaGiftsMgr.getConfig().getActivityId(), targetUserId);
                giftMostUserId = GuaGiftsMgr.getGiftMostUserIdByRelationMap(userGuaRelationDataMap);
            }
        }

        GuaGiftsProto.CrossGuaGiftsGetMostGiftTargetUserRespMsg.Builder resp = GuaGiftsProto.CrossGuaGiftsGetMostGiftTargetUserRespMsg.newBuilder();
        resp.setUserId(giftMostUserId);

        syncResponse.setResponseBuilder(resp);

        return syncResponse;
    }
}
